import os
import shutil
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import changeName
import openpyxl
from openpyxl.reader.excel import load_workbook
import copyData
import buildOptionsProject
import buildScriptProject


class ExcelApp:

    def __init__(self, master):
        self.master = master
        self.master.title("导入系统")
        self.master.geometry("600x180")  # 设置窗口大小
        self.master.configure(bg="#E0FFFF")  # 设置背景颜色

        # 使用ttk库提供更现代的外观
        style = ttk.Style()
        style.theme_use('clam')  # 更改主题为Clam，提供更好的默认样式

        self.open_button = ttk.Button(master, text="打开已有文件", command=self.open_file)
        self.open_button.pack(pady=10, padx=20)

        self.new_button = ttk.Button(master, text="新建Excel文件", command=self.create_new_file)
        self.new_button.pack(pady=10, padx=20)

        self.new_button = ttk.Button(master, text="生成导出结果", command=self.create_output_file)
        self.new_button.pack(pady=10, padx=20)

    def open_file(self):
        # 使用filedialog选择文件
        file_path = filedialog.askopenfilename(title="选择Excel文件",
                                               filetypes=(("Excel Files", "*.xlsx"), ("All Files", "*.*")))
        print(file_path)

        # 检查用户是否取消了文件选择
        if not file_path:
            print("文件选择被取消")
            return None

        try:
            # 使用openpyxl加载Excel文件
            workbook = load_workbook(filename=file_path)
            os.system(file_path)
            print(f"成功打开了文件: {file_path}")
        except Exception as e:
            # 处理可能的异常，如文件格式不正确，文件无法读取等
            print(f"无法打开文件：{file_path}，错误原因：{e}")
            return None
        else:
            return workbook

    def create_new_file(self):
        # 获取保存新文件的路径
        src_file_path = os.path.abspath("./Examples/wanDan.xlsx")
        dst_folder_path = os.path.abspath("./output")
        dst_file_path = os.path.join(dst_folder_path, os.path.basename(src_file_path))

        shutil.copy(src_file_path, dst_file_path)
        os.system(dst_file_path)

    def create_output_file(self):
        # 打开文件对话框选择文件
        file_path = filedialog.askopenfilename(title="选择Excel文件",
                                               filetypes=(("Excel Files", "*.xlsx"), ("All Files", "*.*")))
        if not file_path:
            messagebox.showinfo("提示", "文件选择被取消")
            return None

        try:
            source_folders = ["./Examples/audio", "./Examples/cache", "./Examples/gui", "./Examples/images",
                              "./Examples/saves", "./Examples/tl", "./Examples/music"]
            target_folder = "./output"
            wb = openpyxl.load_workbook(file_path)
            print(f"成功打开了文件: {file_path}")
            buildScriptProject.buildScriptProject(url=file_path)
            buildOptionsProject.buildOptionsProject(url=file_path)
            copyData.copy_folders(source_folders, target_folder)
            changeName.rename_files_in_dir('./output', '.txt', '.rpy')
            output_message = f"生成结果成功，导出文件位于: {os.path.dirname(file_path)}"
            messagebox.showinfo("提示", output_message)
        except Exception as e:
            messagebox.showerror("错误", f"生成过程中出现错误: {e}")
        finally:
            self.master.quit()


if __name__ == "__main__":
    root = tk.Tk()
    app = ExcelApp(root)
    root.mainloop()
